<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_PATTERN.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_PATTERN,$login['staff_auth']);

$dosearch = FALSE ;

$dao = new Class_M_PATTERN;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['PATTERN'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['PATTERN'] = $forms;
}

$wherearr =  array();

/*
if ( isset( $forms['pattern_cd'] ) &&  ( !empty( $forms['pattern_cd'] ) ) ){
	$wherearr[] = sprintf(" pattern_cd =  '%s' ",  $forms['pattern_cd'] );
}
*/
if ( isset( $forms['pattern_name'] ) &&  ( !empty( $forms['pattern_name'] ) ) ){
	$wherearr[] = sprintf(" pattern_name like  '%s' ",  "%".$dao->esc( $forms['pattern_name'] )."%" );
}

if ( isset( $forms['media_flg']  )  &&  ( !empty( $forms['media_flg'] ) ) ){
	$wherearr[] = sprintf(" media_flg =  '%s' ", $dao->esc( $forms['media_flg'] ) );
}

if ( isset( $forms['disp_ng']  )  &&  ( strlen( $forms['disp_ng'] ) > 0 ) ){
	$wherearr[] = sprintf(" disp_ng =  '%s' ",  $dao->esc( $forms['disp_ng'] ) );
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1 );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2 );
}


//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "pattern".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
	$colums = array();
	//列名
	//1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'pattern_cd'   => array( 'パターンコード', 1),
		'pattern_name' => array( 'パターン名',     1),
		'media_flg'    => array( '使用媒体',       2),
		'block'        => array( 'ブロック',       4),
		'disp_ng'      => array( '使用不可',       2),
		'c_time'       => array( '新規日時',       3),
		'u_time'       => array( '更新日時',       3),
		'm_user_name'  => array( '更新者名',       1)
	);
/*
1	システムID
2	パターン名
3	使用媒体
4	ブロック
5	非表示
6	登録日時
7	更新日時
8	更新者
*/
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		//
		$data[$k]["media_flg"] =  $aoki_pattern_flags[$v["media_flg"]] ;
		$data[$k]["disp_ng"]   =  $v["disp_ng"] == '1' ? $aoki_disp_flags['1'] : $aoki_disp_flags['0'];
		$data[$k]["c_time"]    =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]    =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data',  $data);
if( isset($page) )  $smarty->assign('page',  $page);

$dspflags[""] = "すべて";
foreach ($aoki_disp_flags as $k => $v) {
	$dspflags[$k] = $v;
}

$smarty->assign('dspflags', $dspflags );

$flags[""] = "すべて";
foreach ($aoki_pattern_flags as $k => $v) {
	$flags[$k] = $v;
}

$smarty->assign('flags', $flags );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mpattern_index.html');

?>